<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="areaCode">
        <el-select v-model="queryParams.areaCode" filterable placeholder="请选择区域" clearable size="small">
          <el-option
            v-for="item in regionQueryList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="停用" prop="closed">
        <el-select v-model="queryParams.closed" placeholder="请选择停用" clearable size="small">
          <el-option
            v-for="dict in dict.type.is_closed"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>


    <el-table v-loading="loading" :data="guestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column label="编码" prop="code"/>
      <el-table-column label="客户" prop="name"/>
      <el-table-column label="区域" prop="areaid">
        <template slot-scope="scope">
          {{ regionMatching(scope.row.areaid) }}
        </template>
      </el-table-column>
      <el-table-column label="联系人" prop="contractor"/>
      <el-table-column label="联系人手机" prop="phone"/>
      <el-table-column label="地址" prop="address"/>
      <el-table-column label="联系人固话" prop="tel"/>
      <el-table-column label="授信额度" prop="creditamount"/>
      <el-table-column label="账户余额" prop="amount"/>
      <el-table-column label="登录用户" prop="userName"/>
      <el-table-column label="停用" prop="closed">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_closed" :value="scope.row.closed"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  listGuest,
  getGuest,
  delGuest,
  addGuest,
  updateGuest,
  bindUserList,
  bindUser,
  generateUser, batchGenerateUser, batchGenerateUserAll, listAllGuest
} from "@/api/system/guest";
import {getRegionQuery, listRegionAll} from "@/api/system/region";
import {listDriverAll, listSalesmanAll} from "@/api/system/employee";
import {getToken} from "@/utils/auth";

export default {
  name: "Guest",
  dicts: ['is_closed'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      guestList: [],
      //司机列表
      driverList: [],
      //员工列表
      employeeList: [],
      //区域列表
      regionList: [],
      regionQueryList:[],
      //绑定用户列表
      bindUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否弹出绑定用户层
      bindOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        areaCode: null,
        closed: null,
        noMonthOrder:true
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getRegionQueryList();
    this.getRegionList();
    this.getDriverList();

  },
  methods: {
    getbindUserList() {

      bindUserList().then(response => {
        this.bindUserList = response.data;
      })
    },
    getRegionQueryList() {
      getRegionQuery().then(response => {
        this.regionQueryList = response.data;
      });
    },

    getRegionList() {
      listRegionAll().then(response => {
        this.regionList = response.data;
      });
    },
    getDriverList() {
      listDriverAll().then(response => {
        this.driverList = response.data;
        this.getSalesmanList();
      });
    },
    getSalesmanList() {
      listSalesmanAll().then(response => {
        this.employeeList = response.data;
      });
    },
    regionMatching(areaid) {
      let name = "-";
      this.regionList.map(item => {
        if (areaid == item.areaid) {
          name = item.county ;
          return
        }
      })
      return name;
    },
    employeeMatching(employeeId) {
      let name = "-";
      this.employeeList.map(item => {
        if (employeeId == item.empid) {
          name = item.name;
          return
        }
      })
      return name;
    },

    driverMatching(employeeId) {
      let name = "-";
      this.driverList.map(item => {
        if (employeeId == item.empid) {
          name = item.name;
          return
        }
      })
      return name;
    },
    /** 查询客户列表 */
    getList() {
      this.loading = true;
      listGuest(this.queryParams).then(response => {
        this.guestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        gid: null,
        code: null,
        name: null,
        areaid: null,
        empid: null,
        contractor: null,
        phone: null,
        address: null,
        tel: null,
        url: null,
        driverid: null,
        creditamount: null,
        amount: null,
        remark: null,
        closed: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.bindform = {
        gid: null,
        userid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.gid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const gid = row.gid || this.ids
      getGuest(gid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.gid != null) {
            updateGuest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGuest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /**绑定用户**/
    bindUser() {
      if (!this.bindform.userid) {
        this.$modal.msgWarning("请选择本地的用户");
        return
      }
      if (!this.bindform.gid) {
        this.$modal.msgWarning("提交失败");
        return
      }
      bindUser(this.bindform).then(response => {
        let data = response.data
        if (data.code == 1) {
          this.$modal.msgSuccess(data.msg);
        } else {
          this.$modal.msgWarning(data.msg);
        }
        this.getList();
      }).catch(() => {
      });
    },
    /**解绑定用户**/
    unBindUser() {
      if (!this.bindform.gid) {
        this.$modal.msgWarning("提交失败");
        return
      }
      bindUser(this.bindform).then(response => {
        let data = response.data
        if (data.code == 1) {
          this.$modal.msgSuccess(data.msg);
        } else {
          this.$modal.msgWarning(data.msg);
        }
        this.getList();
      }).catch(() => {
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const gids = row.gid || this.ids;
      this.$modal.confirm('是否确认删除客户编号为"' + gids + '"的数据项？').then(function () {
        return delGuest(gids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
